//
//  copyLabel.m
//  ChatDemo-UI2.0
//
//  Created by zz on 16/5/23.
//  Copyright © 2016年 zz. All rights reserved.
//

#import "copyLabel.h"

@implementation copyLabel


-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self attachTapHandler];
    }
    return self;
}

-(void)awakeFromNib{
    
    [super awakeFromNib];
    [self attachTapHandler];
}

-(BOOL)canBecomeFirstResponder{
    
    return YES;
}

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    
    if (action == @selector(copy1:)) {
        
        return YES;
        
    }
    
    return NO;
    //return action == @selector(copy:);
    //return YES;
}

//UILabel默认是不接收事件的，我们需要自己添加touch事件

-(void)attachTapHandler

{
    
    self.userInteractionEnabled = YES;  //用户交互的总开关
    
    UILongPressGestureRecognizer *longPregress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    longPregress.minimumPressDuration = 0.5f;
    

    [self addGestureRecognizer:longPregress];

}

-(void)handleTap:(UIGestureRecognizer*) recognizer

{
    
    [self becomeFirstResponder];
    
    UIMenuItem *copyLink = [[UIMenuItem alloc] initWithTitle:@"复制"action:@selector(copy1:)];
    
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:copyLink, nil]];
    
    [[UIMenuController sharedMenuController] setTargetRect:self.frame inView:self.superview];
    
    [[UIMenuController sharedMenuController] setMenuVisible:YES animated: NO];
}

-(void)copy1:(id)sender{
    
    UIPasteboard *board = [UIPasteboard generalPasteboard];
    board.string = self.text;

}

@end
